//
//  FWCommonTools.m
//  GPU滤镜效果
//
//  Created by JackAndney on 2018/1/23.
//  Copyright © 2018年 xiao. All rights reserved.
//

#import "FWCommonTools.h"

#if __has_include(<GPUImage/GPUImage.h>)
#import <GPUImage/GPUImagePicture.h>
#endif

@implementation FWCommonTools

+ (UIImage *)getImageWithFilter:(GPUImageFilter *)filter image:(UIImage *)originalImage method:(NSString *)methodName value:(float)value
{
    UIImage *image = originalImage;

    if ([filter respondsToSelector:NSSelectorFromString(methodName)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [filter performSelector:NSSelectorFromString(methodName) withObject:[NSString stringWithFormat:@"%f", value]];
#pragma clang diagnostic pop
    } else {
        return nil;
    }

    [filter forceProcessingAtSize:originalImage.size];
    GPUImagePicture *stillImage = [[GPUImagePicture alloc] initWithImage:image];
    [stillImage addTarget:filter];
    [stillImage processImage];
    [filter useNextFrameForImageCapture];

    return [filter imageFromCurrentFramebuffer];
}

+ (UIImage *)fw_imageWithName:(NSString *)imgName
{
    NSBundle *mainBundle = [NSBundle bundleForClass:[self class]];
    NSBundle *resourcesBundle = [NSBundle bundleWithPath:[mainBundle pathForResource:@"FWFilterResource" ofType:@"bundle"]];

    if (resourcesBundle == nil) {
        resourcesBundle = mainBundle;
    }

    UIImage *image = [UIImage imageNamed:imgName inBundle:resourcesBundle compatibleWithTraitCollection:nil];
    if (image) {
        return image;
    } else {
        return [UIImage imageNamed:imgName];
    }
}

@end
